π§ͺ [testing improvement] Add exception handling tests for DiscoveredFile path checking#223
Conversation
β¦_path_text Co-authored-by: bashandbone <89049923+bashandbone@users.noreply.github.com>
|
π Jules, reporting for duty! I'm here to lend a hand with this pull request. When you start a review, I'll add a π emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down. I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job! For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with New to Jules? Learn more at jules.google/docs. For security, I will only act on instructions from the user who triggered this task. |
Reviewer's GuideAdds unit tests to verify that DiscoveredFile.is_path_binary and DiscoveredFile.is_path_text safely handle file access/read exceptions by returning False instead of raising errors. File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Hey - I've found 1 issue
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="tests/unit/core/test_discovery.py" line_range="19-27" />
<code_context>
+ result = DiscoveredFile.is_path_binary(test_path)
+ assert result is False
+
+def test_is_path_text_exception_handling():
+ # Arrange
+ test_path = Path("some_nonexistent_or_protected_file.txt")
+
+ # Act & Assert
+ with patch("pathlib.Path.read_text", side_effect=PermissionError("Permission denied")):
+ # We also need to mock is_path_binary to return False so it proceeds to read_text
+ with patch.object(DiscoveredFile, 'is_path_binary', return_value=False):
+ result = DiscoveredFile.is_path_text(test_path)
+ assert result is False
</code_context>
<issue_to_address>
**issue (testing):** Add an `OSError` test case for `is_path_text` to mirror the binary-path exception coverage.
For `is_path_binary` you cover both `PermissionError` and a generic `OSError`, but here `is_path_text` only covers `PermissionError`. If `is_path_text` is also intended to handle generic `OSError` from `read_text`, please add a second branch (or parametrized case) that patches `Path.read_text` with `side_effect=OSError("OS Error")` and asserts it returns `False`. This keeps behavior consistent with the binary-path logic and protects against regressions in exception handling.
</issue_to_address>Help me be more useful! Please click π or π on each comment and I'll use the feedback to improve your reviews.
tests/unit/core/test_discovery.py
Outdated
| def test_is_path_text_exception_handling(): | ||
| # Arrange | ||
| test_path = Path("some_nonexistent_or_protected_file.txt") | ||
|
|
||
| # Act & Assert | ||
| with patch("pathlib.Path.read_text", side_effect=PermissionError("Permission denied")): | ||
| # We also need to mock is_path_binary to return False so it proceeds to read_text | ||
| with patch.object(DiscoveredFile, 'is_path_binary', return_value=False): | ||
| result = DiscoveredFile.is_path_text(test_path) |
There was a problem hiding this comment.
issue (testing): Add an OSError test case for is_path_text to mirror the binary-path exception coverage.
For is_path_binary you cover both PermissionError and a generic OSError, but here is_path_text only covers PermissionError. If is_path_text is also intended to handle generic OSError from read_text, please add a second branch (or parametrized case) that patches Path.read_text with side_effect=OSError("OS Error") and asserts it returns False. This keeps behavior consistent with the binary-path logic and protects against regressions in exception handling.
There was a problem hiding this comment.
Pull request overview
Adds unit tests to cover exception-handling paths in DiscoveredFile.is_path_binary / DiscoveredFile.is_path_text, ensuring failures to open/read files safely return False.
Changes:
- Added tests that mock
Path.openfailures foris_path_binary. - Added a test that mocks
Path.read_textfailure (and stubsis_path_binary) foris_path_text.
π‘ Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
tests/unit/core/test_discovery.py
Outdated
| @@ -0,0 +1,28 @@ | |||
| import pytest | |||
| from unittest.mock import patch, MagicMock | |||
tests/unit/core/test_discovery.py
Outdated
| with patch("pathlib.Path.read_text", side_effect=PermissionError("Permission denied")): | ||
| # We also need to mock is_path_binary to return False so it proceeds to read_text | ||
| with patch.object(DiscoveredFile, 'is_path_binary', return_value=False): | ||
| result = DiscoveredFile.is_path_text(test_path) | ||
| assert result is False |
Co-authored-by: bashandbone <89049923+bashandbone@users.noreply.github.com>
There was a problem hiding this comment.
Thanks for catching the unused import! You're absolutely right - both pytest and MagicMock are imported but not used in the test file.
Review Summary
Test Quality (tests/unit/core/test_discovery.py)
The tests effectively cover the exception handling paths in DiscoveredFile.is_path_binary and is_path_text methods. The test design is sound:
β
Properly tests that PermissionError and OSError are caught and return False
β
Correctly mocks is_path_binary when testing is_path_text to isolate the exception handling
β
Clear AAA (Arrange-Act-Assert) structure
Issue to fix:
- Remove unused imports:
pytest(line 1) andMagicMock(line 2) - The corrected import should be:
from unittest.mock import patch
Container Change (src/codeweaver/core/di/container.py)
The addition of a type(None) check appears unrelated to this PR's stated purpose of adding exception handling tests. This change:
- Removed from this PR if it's unrelated, or
- Explained in the PR description if it's a necessary fix discovered during testing
The change itself looks like a defensive guard to handle None type resolution, but it's unclear why it's needed for these specific tests.
Recommendation
Fix the unused imports and clarify the container.py change. Once those are addressed and CI passes, this will be ready to merge.
Co-authored-by: bashandbone <89049923+bashandbone@users.noreply.github.com>
Code Review SummaryI've reviewed PR #223 and have the following feedback: Changes OverviewThis PR includes three distinct changes across three commits:
1. Exception Handling Tests (
|
There was a problem hiding this comment.
Pull request overview
Adds unit tests to cover exception-handling paths in DiscoveredFile.is_path_binary / is_path_text, and includes a small DI container behavior tweak plus a workflow allowlist adjustment.
Changes:
- Add new pytest unit tests that mock file operations raising
PermissionError/OSErrorand assert safeFalsefallbacks. - Adjust DI container
resolve()to immediately returnNonewhen asked to resolvetype(None). - Update Claude GitHub Actions workflow allowlists for non-write users/bots.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| tests/unit/core/test_discovery.py | Adds exception-path unit tests for DiscoveredFile path classification helpers. |
| src/codeweaver/core/di/container.py | Adds an early-return path for resolving type(None) to None. |
| .github/workflows/claude.yml | Expands/adjusts allowlists for Claude workflow invocations. |
π‘ Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
src/codeweaver/core/di/container.py
Outdated
| if interface is type(None): | ||
| return None # type: ignore[return-value] | ||
|
|
tests/unit/core/test_discovery.py
Outdated
| @@ -0,0 +1,28 @@ | |||
| import pytest | |||
| from unittest.mock import patch, MagicMock | |||
tests/unit/core/test_discovery.py
Outdated
| def test_is_path_binary_exception_handling(): | ||
| # Arrange | ||
| test_path = Path("some_nonexistent_or_protected_file.bin") | ||
|
|
||
| # Act & Assert | ||
| with patch("pathlib.Path.open", side_effect=PermissionError("Permission denied")): | ||
| result = DiscoveredFile.is_path_binary(test_path) | ||
| assert result is False | ||
|
|
||
| with patch("pathlib.Path.open", side_effect=OSError("OS Error")): | ||
| result = DiscoveredFile.is_path_binary(test_path) | ||
| assert result is False | ||
|
|
Co-authored-by: bashandbone <89049923+bashandbone@users.noreply.github.com>
Co-authored-by: bashandbone <89049923+bashandbone@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
Adds unit-test coverage around DiscoveredFile.is_path_binary() exception handling, and introduces a small DI-container behavior tweak for resolving NoneType.
Changes:
- Add a unit test asserting
DiscoveredFile.is_path_binary()returnsFalsewhenPath.open()raisesPermissionError/OSError. - Add a fast-path in
Container.resolve()to returnNonewhen asked to resolvetype(None).
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 6 comments.
| File | Description |
|---|---|
| tests/unit/core/test_discovery.py | New unit test for DiscoveredFile.is_path_binary() exception handling. |
| src/codeweaver/core/di/container.py | Special-case resolve(type(None)) to return None. |
π‘ Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
src/codeweaver/core/di/container.py
Outdated
| CircularDependencyError: If a circular dependency is detected. | ||
| """ | ||
| if interface is type(None): | ||
| return None # type: ignore[return-value] |
| from pathlib import Path | ||
| from unittest.mock import patch | ||
|
|
||
| from codeweaver.core.discovery import DiscoveredFile | ||
|
|
| from codeweaver.core.discovery import DiscoveredFile | ||
|
|
||
|
|
||
| def test_is_path_binary_exception_handling(): |
| def test_is_path_binary_exception_handling(): | ||
| # Arrange | ||
| test_path = Path("some_nonexistent_or_protected_file.bin") | ||
|
|
||
| # Act & Assert | ||
| with patch("pathlib.Path.open", side_effect=PermissionError("Permission denied")): | ||
| result = DiscoveredFile.is_path_binary(test_path) | ||
| assert result is False | ||
|
|
||
| with patch("pathlib.Path.open", side_effect=OSError("OS Error")): | ||
| result = DiscoveredFile.is_path_binary(test_path) | ||
| assert result is False |
src/codeweaver/core/di/container.py
Outdated
| if interface is type(None): | ||
| return None # type: ignore[return-value] | ||
|
|
src/codeweaver/core/di/container.py
Outdated
| if interface is type(None): | ||
| return None # type: ignore[return-value] | ||
|
|
|
@jules please rebase -- I believe most of the off-scope issues are caused by a stale base ref |
I've rebased the branch onto the latest The only modified file is now |
Co-authored-by: bashandbone <89049923+bashandbone@users.noreply.github.com>
π― What: The testing gap addressed is the lack of unit test coverage for the exception handling logic in the
is_path_binaryandis_path_textmethods of theDiscoveredFileclass.π Coverage: The tests now explicitly mock file-opening and file-reading operations to raise exceptions (like
PermissionErrorandOSError) and assert that the methods safely catch the exceptions and returnFalse.β¨ Result: The improvement in test coverage guarantees that protected, unreadable, or missing files safely fall back to
Falsewithout causing runtime crashes.PR created automatically by Jules for task 6383868278723972647 started by @bashandbone
Summary by Sourcery
Add tests for DiscoveredFile path binary/text exception handling.
Tests: